/**
 * Created by JF-Z on 2017/12/7.
 */
angular.module('module.devup')
    .controller('ShopListController', ShopListController);
ShopListController.$inject = ['$state','$http','DevupService'];
function ShopListController($state, $http,DevupService) {
    var vm = this;
    vm.devDetail = devDetail;
    vm.verList = [];
    vm.verLists = [];
    vm.upLists=[];
    vm.upList=[];
    vm.pageSize =10;
    vm.currentPage = 1;
    vm.update = update;

    ///////////////////////////////////////////////////

//调用数据
    DevupService.getShop().$promise.then(function (data) {
        vm.shopList = data;
       for(var i=0;i<vm.shopList.length;i++){
           vm.shopList[i].dev = [];
            con(vm.shopList[i].shopid,i)
        }
    });
    function con(id,i) {
        DevupService.getDev(id).$promise.then(function (data) {
            vm.devups=data;
            version(vm.devups,0,i);
        },function (err) {

        })
    }
    function version(list,i,j){
        if(i>=list.length){
            return;
        }
        var devlist=list[i];
        DevupService.getVer(devlist.devid).$promise.then(function(info){
            vm.verList.push(info);
            vm.shopList[j].dev.push(info)
            version(list,i+1,j)
        },function(){
            version(list,i+1,j)
        })

    }

//获取升级数据
    function verDatas(ver) {
        var info = {
            devid: ver.devid,
            hardv: ver.hardver,
            softv: ver.softver
        };
        var verInfo = [];
        verInfo.push(info);
        vm.upList={};
        DevupService.getVerUp(verInfo).$promise.then(function(data){
            vm.upList=data[0];
            vm.upLists.push(vm.upList)
        })

    }

//城市检测区域
    $(function () {
        //原生城市-联动
        var select = $('.city-picker-select').cityPicker({
            dataJson: cityData,
            renderMode: false,
            autoSelected: true
        });

        //设置城市
        select.setCityVal([{
            'id': '350000',
            'name': '福建省'
        }, {
            'id': '350100',
            'name': '福州市'
        }, {
            'id': '350121',
            'name': '闽侯县'
        }]);
//省份检测
        $('.province').bind('change',function () {

            var pro = $(".province").find("option:selected").text();
            if(pro!=='请选择省份'){
                $('.shop_list_table tr:not(".shop_table_tr")').hide().filter(':contains("'+pro+'")').show();

            }else {
                $('.shop_list_table tr:not(".shop_table_tr")').show();


            }
        })
//市区检测
        $('.city').bind('change',function () {
            var city = $(".city").find("option:selected").text();
            $('.shop_list_table tr:not(".shop_table_tr")').hide().filter(':contains("'+city+'")').show();

        })

//县区检测
   $('.district').bind('change',function () {
            var district = $(".district").find("option:selected").text();

            $('.shop_list_table tr:not(".shop_table_tr")').hide().filter(':contains("'+district+'")').show();
        })

    })

//通过shopid调用数据 跳转页面
    function devDetail(shopid) {
        $state.go("deviceList", {shopid: shopid});
    }

//back返回
    vm.back = function () {
        history.go(-1);
    }
/*选择框开始*/

    vm.selection_bind=[];//存贮选中对象数组
    var aim = document.getElementsByName('chb');//获取本页全部数据CheckBox
//选择单条数据
    vm.prepare_data=function(shop){
        console.log(shop)
        vm.currentmaster = false;
        check(shop);
        console.log( vm.selection_bind)
    }
//全选数据
    vm.prepare_alldata=function(master){
        checkall(master);
        console.log( vm.selection_bind)
    }
//选择本页数据
    vm.prepare_currentall = function (currentmaster) {
        if(vm.master){
            checkall(false);
        }
        console.log(currentmaster)

        if(currentmaster){
            if(vm.selection_bind.length != 0){
                for(var i = 0;i < vm.selection_bind.length;i++){
                    for(var j = 0;j <vm.shopList.length;j++){
                        if(vm.selection_bind[i].shopid == vm.shopList[j].shopid){
                            vm.shopList[j].checked = false;
                            break;
                        }
                    }
                }
                vm.selection_bind = [];
            }
            for(var i = 0;i < aim.length;i++){
                for(var j = 0;j < vm.shopList.length;j++){
                    if(aim[i].id == vm.shopList[j].shopid){
                        vm.shopList[j].checked = true;
                        check(vm.shopList[j]);
                        break;
                    }
                }
            }
        }else {
            for(var i = 0;i < aim.length;i++){
                for(var j = 0;j < vm.shopList.length;j++){
                    if(aim[i].id == vm.shopList[j].shopid){
                        vm.shopList[j].checked = false;
                        check(vm.shopList[j]);
                        break;
                    }
                }
            }
            vm.selection_bind = [];
        }
        console.log( vm.selection_bind)
    }
//单独选择方法
    function check(shop) {
        vm.judge = false;
        for(var i = 0;i < vm.selection_bind.length;i++){
            if(vm.selection_bind[i].shopid == shop.shopid){
                vm.judge = true;
                vm.index = i;
                break;
            }
        }
        if(shop.checked && !vm.judge){
            vm.selection_bind.push(shop);
        }else if(!shop.checked && vm.judge){
            vm.selection_bind.splice(vm.index,1);
        }
        if(vm.selection_bind.length === vm.shopList.length){
            vm.master = true;
        }else {
            vm.master = false;
        }
    }
//全选方法
    function checkall(master) {
        vm.selection_bind = [];
        if(master){
            angular.forEach(vm.shopList,function (shop) {
                shop.checked = true
                vm.selection_bind.push(shop);
            })
        }else{
            angular.forEach(vm.shopList,function (shop) {
                shop.checked = false
                vm.selection_bind = [];
            })
        }

    }
//页数更改取消本页全选
    vm.pageChangeHandler = function (newpage) {
        console.log("点击"+newpage+"页");
        //未全选则清空存贮数组
        if(vm.currentmaster){
            vm.currentmaster = false;
            vm.prepare_currentall(false)
        }
        console.log( vm.selection_bind)
    }

/*选择框结束*/
    /*批量升级*/
    vm.visible = false;
    vm.upBatch=function () {
        vm.visiable=!vm.visiable
    }

/*弹窗*/
    vm.verPop=function (shop) {
        $('.shopBgPop,.shopPop').show();
        cons(shop.shopid);
        function cons(id) {
            DevupService.getDev(id).$promise.then(function (data) {
                vm.devups=data;
                console.log(vm.devups)
                version(vm.devups ,0);
            },function (err) {

            })
        }
        function version(list,i){
            if(i>=list.length){
                return;
            }
            var devlist=list[i];
            DevupService.getVer(devlist.devid).$promise.then(function(info){
                vm.verLists.push(info);
                version(list,i+1)
            },function(){
                version(list,i+1)
            })
        }


    }
   vm.cancel=function () {
       $('.shopBgPop,.shopPop').hide(500);
   }
 //获取多组数据
    function verDatas(ver) {
        var info = {
            devid: ver.devid,
            hardv: ver.hardver,
            softv: ver.softver
        };
        var verInfo = [];
        verInfo.push(info);
        vm.upList={};
        DevupService.getVerUp(verInfo).$promise.then(function(data){
            vm.upList=data[0];
            vm.upLists.push(vm.upList)
        })

    }
//确定升级
    function update() {
        var list = [];
        for(var i = 0;i < vm.selection_bind.length;i++){
            for(var j = 0;j < vm.selection_bind[i].dev.length;j++){
                list.push(vm.selection_bind[i].dev[j])
            }
        }
        console.log(list)
        for(var i=0;i<list.length;i++){
            verDatas(list[i]);
        }
        DevupService.getDevUp(vm.upLists).$promise.then(function (data) {
            vm.devUp=data;
             alert("上传成功")
        }
        ,function (error) {
                alert("上传失败")
        });
    }
//确定升级单个店铺
    vm.upCon=function () {
        for(var i=0;i<vm.verLists.length;i++){
            verDatas(vm.verLists[i]);
        }
        DevupService.getDevUp(vm.upLists).$promise.then(function (data) {
            vm.devUp=data;
            alert("上传成功")
        },function (error) {
            alert("上传失败")
        });
     $('.shopBgPop,.shopPop').hide();
    }


}

